package com.skyhub.controller;

import com.skyhub.common.domain.pojo.Result;
import com.skyhub.common.domain.pojo.User;
import com.skyhub.service.EditorService;
import com.skyhub.utils.MinioUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/editor")
@CrossOrigin
public class EditorController {

    @Autowired
    private MinioUtils minioUtils;

    @Autowired
    private EditorService editorService;

    @PostMapping("/uploadAvatar/{userId}")
    public Result uploadAvatar(@RequestParam("file") MultipartFile file,@PathVariable("userId")Integer userId){

        log.info("上传文件：{}",file.getOriginalFilename());

        String url="/Avatars/"+UUID.randomUUID().toString()+"-"+file.getOriginalFilename();

        minioUtils.upload(file,url);

        url="http://192.168.44.133:9000/skyhub-bucket"+url;

        editorService.uploadAvatar(url,userId);

        return Result.success("上传成功","头像修改成功");
    }

    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody User user){

        editorService.updateUser(user);

        return Result.success("用户身份修改成功","3秒后退出登录");
    }
}
